package pers.rcklos.myserver.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import pers.rcklos.myserver.biz.PaperBiz;
import pers.rcklos.myserver.entity.PaperItem;
import pers.rcklos.myserver.entity.SpUser;

@Controller
public class PaperController {
	private PaperBiz paperBiz;
	
	private static final ConcurrentLinkedQueue<PaperItem> paperQueue = 
			new ConcurrentLinkedQueue<>();
	
	@GetMapping("/paper/query_all")
	public String queryAllPapers(@RequestParam(defaultValue = "0")int start, Model model) {
//		ArrayList<PaperItem> list = paperBiz.queryAllPapers();
		model.addAttribute("page", start);
		ArrayList<PaperItem> list = paperBiz.queryPageOfPapers(start, 10);
		model.addAttribute("papers", list);
		model.addAttribute("query_size", list.size());
		// 装配head部分
		model.addAttribute("thead", 1);			// 纸条表
		return "admin";
	}
	
	@GetMapping("/paper/query")
	public String queryPaper(HttpSession session) {
		SpUser user = (SpUser) session.getAttribute("user");
		if(user == null) {
			return "redirect:../user/login";
		}
		
		ArrayList<PaperItem> list = paperBiz.queryPapers(user);
		session.setAttribute("userPapers", list);
		
		return "user";
//		resp.sendRedirect("user.jsp");
	}
	
	@PostMapping("/paper/send")
	@ResponseBody
	public String sendPaper(String title, String content, HttpSession session) {
		if((title==null||content==null) || ("".equals(title)||"".equals(content))) {
			return "err";
		}
		
		SpUser user = (SpUser) session.getAttribute("user");
		if(user == null) {
			return "err: lost user token";
		}
		
		PaperItem paper = paperBiz.addPaper(user, title, content);
		if(paper==null) {
			return "err: add fail";
		}
		
		paperQueue.add(paper);
		return "ok";
	}
	
	@RequestMapping("/paper/delete")
	@ResponseBody
	public String deletePaper(String pid, HttpSession session) {
		if(pid==null||pid.equals("")) {
			return "param is invalid";
		}
		
		SpUser user = (SpUser) session.getAttribute("user");
		if(user == null) {
			return "lost user";
		}
		
		boolean isSuccess = paperBiz.deletePaper(user, Integer.parseInt(pid));
		return isSuccess?"success":"fail!";
	}
	
	@RequestMapping(value="/paper/dequeue", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String dequeuePaper(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		if(!paperQueue.isEmpty()) {
			PaperItem paper = paperQueue.poll();
			JSONObject json = (JSONObject) JSONObject.toJSON(paper);
			json.put("msg", "ok");
			return json.toJSONString();
		}
		JSONObject jsonNULL = new JSONObject();
		jsonNULL.put("msg", "null");
		return jsonNULL.toJSONString();
	}
	
	@Autowired
	public void setPaperBiz(PaperBiz paperBiz) {
		this.paperBiz = paperBiz;
	}
}
